package com.workpaper.app.workpaper.daily.view.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;

import com.workpaper.app.cache.resource.ViewInfoCache;
import com.workpaper.app.consts.common.CommonViewConsts;
import com.workpaper.app.workpaper.daily.view.listener.DeleteDailyWorkpaperListener;
import com.workpaper.app.workpaper.daily.view.listener.EditDailyWorkpaperListener;
import com.workpaper.app.workpaper.daily.view.listener.ShowDailyWorkpaperListener;
/**
 * 为TableViewer中的行设置按钮
 * @author zhouyujun
 */
public class DailyWorkpaperViewUtils {
	
	private HashMap<TableViewer, List<TableEditor>>  editorMap = new HashMap<TableViewer, List<TableEditor>>();
	
	private static class Holder{
		private static DailyWorkpaperViewUtils INSTANCE = new DailyWorkpaperViewUtils();
	}
	
	
	private DailyWorkpaperViewUtils() {
		super();
	}
	
	public static DailyWorkpaperViewUtils getInstance() {
		return Holder.INSTANCE;
	}

	/**
	 * 设置行操作按钮：
	 * （1）先从缓存中清理掉TableViewer之前的TableEditor及按钮
	 * （2）为table的每一行添加新的TableEditor和Button
	 * （3）缓存
	 * @param tableViewer
	 */
	public void addRowOperatingButtons(TableViewer tableViewer) {
		Table table = tableViewer.getTable();
		List<TableEditor> editorList = new ArrayList<TableEditor>();
		List<TableEditor> oldList = editorMap.get(tableViewer);
		if(oldList != null) {
			//清理之前的按钮
			for (TableEditor tableEditor : oldList) {
				Control control = tableEditor.getEditor();	//销毁按钮避免再次创建按钮的时候，通过setData()设置的数据混乱
				control.dispose();
				tableEditor.dispose();
			}
			oldList.clear();
		}
		TableItem[] items = tableViewer.getTable().getItems();
		for (TableItem item : items) {
			addShowInfoBtn(table, editorList, item);
			
			addEditButton(tableViewer, editorList, item);
			
			addDeleteButton(tableViewer, editorList, item);
		}
		editorMap.put(tableViewer, editorList);
	}
	/**
	 * 删除按钮
	 * @param tableViewer
	 * @param editorList
	 * @param item
	 */
	private void addDeleteButton(TableViewer tableViewer, List<TableEditor> editorList, TableItem item) {
		TableEditor delBtnEditor = new TableEditor(tableViewer.getTable());
		Button delBtn = new Button(tableViewer.getTable(), SWT.BUTTON2);
		delBtn.setText("删除");
		String id = item.getText(5);
		delBtn.setData(id);
		delBtn.addSelectionListener(new DeleteDailyWorkpaperListener());
		delBtn.pack();
		delBtnEditor.minimumWidth = delBtn.getSize().x;	//Editor的宽度 = Button的宽度
		delBtnEditor.minimumHeight = delBtn.getSize().y;	//Editor的高度 = Button的高度
		delBtnEditor.horizontalAlignment = SWT.RIGHT;	//右侧
		delBtnEditor.setEditor(delBtn, item, tableViewer.getTable().getColumnCount() - 1);	
		editorList.add(delBtnEditor);
	}
	/**
	 * 编辑按钮
	 * @param tableViewer
	 * @param editorList
	 * @param item
	 */
	private void addEditButton(TableViewer tableViewer, List<TableEditor> editorList, TableItem item) {
		TableEditor editBtnEditor = new TableEditor(tableViewer.getTable());
		Button editBtn = new Button(tableViewer.getTable(), SWT.BUTTON2);
		editBtn.setText("编辑");
		String id = item.getText(5);
		editBtn.setData(id);
		editBtn.addSelectionListener(new EditDailyWorkpaperListener());
		editBtn.pack();
		editBtnEditor.minimumWidth = editBtn.getSize().x;	//Editor的宽度 = Button的宽度
		editBtnEditor.minimumHeight = editBtn.getSize().y;	//Editor的高度 = Button的高度
		editBtnEditor.horizontalAlignment = SWT.CENTER;	//居中
		editBtnEditor.setEditor(editBtn, item, tableViewer.getTable().getColumnCount() - 1);	
		editorList.add(editBtnEditor);
	}
	
	
	/**
	 * 新增查看按钮
	 * @param tableViewer
	 * @param editorList
	 * @param item
	 */
	private void addShowInfoBtn(Table table, List<TableEditor> editorList, TableItem item) {
		TableEditor showBtnEditor = new TableEditor(table);
		Button showInfoBtn = new Button(table, SWT.BUTTON2);
		showInfoBtn.setText(ViewInfoCache.get(CommonViewConsts.ButtonText_Show));
		String id = item.getText(5);
		showInfoBtn.setData(id);
		showInfoBtn.addSelectionListener(new ShowDailyWorkpaperListener());
		showInfoBtn.pack();
		showBtnEditor.minimumWidth = showInfoBtn.getSize().x;	//Editor的宽度 = Button的宽度
		showBtnEditor.minimumHeight = showInfoBtn.getSize().y;	//Editor的高度 = Button的高度
		showBtnEditor.horizontalAlignment = SWT.LEFT;	//左侧
		showBtnEditor.setEditor(showInfoBtn, item, table.getColumnCount() - 1);	
		editorList.add(showBtnEditor);
	}
}
